package com.example.demo.service;

import com.example.demo.entity.GoodsItem;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GoodsManageService {
    // 创建线程安全的Map，模拟商品信息的存储
    private Map<Long, GoodsItem> goods =
            Collections.synchronizedMap(new HashMap<Long, GoodsItem>());

    public GoodsItem addGood(GoodsItem good){
        goods.put(good.getId(),good);
        return good;
    }

    public void removeGood(long id){
        goods.remove(id);
    }

    public void updateGood(long id,GoodsItem good){
        GoodsItem good1=goods.get(id);
        good1.setName(good.getName());
        good1.setPrice(good.getPrice());
        good1.setNum(good.getNum());
        goods.put(id,good1);
    }
    public GoodsItem getGood(long id){

        return goods.get(id);
    }
    public List<GoodsItem> findGoods(String name){
        List<GoodsItem> list = new ArrayList<GoodsItem>();
        for(GoodsItem item:goods.values()){
            if(name!=null&&!item.getName().contains(name)){
                continue;
            }
            list.add(item);
        }
        return list;
    }

}
